{% extends 'admin/base.html' %}

{% block title %}添加自行车 - 管理后台{% endblock %}
{% block page_title %}添加自行车{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="bi bi-plus-circle"></i> 添加自行车</h2>
            <a href="{% url 'bike:admin_bike_list' %}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left"></i> 返回自行车管理
            </a>
        </div>

        <div class="card">
            <div class="card-body">
                {% if messages %}
                    {% for message in messages %}
                        <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                            {{ message }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    {% endfor %}
                {% endif %}
                
                <form method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.model.id_for_label }}" class="form-label">型号 *</label>
                                {{ form.model }}
                                {% if form.model.errors %}
                                <div class="text-danger">{{ form.model.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.category.id_for_label }}" class="form-label">类别 *</label>
                                {{ form.category }}
                                {% if form.category.errors %}
                                <div class="text-danger">{{ form.category.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.owner_name.id_for_label }}" class="form-label">所有人姓名 *</label>
                                {{ form.owner_name }}
                                {% if form.owner_name.errors %}
                                <div class="text-danger">{{ form.owner_name.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.owner_student_id.id_for_label }}" class="form-label">学号</label>
                                {{ form.owner_student_id }}
                                {% if form.owner_student_id.errors %}
                                <div class="text-danger">{{ form.owner_student_id.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.class_name.id_for_label }}" class="form-label">班级</label>
                                {{ form.class_name }}
                                {% if form.class_name.errors %}
                                <div class="text-danger">{{ form.class_name.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.phone_number.id_for_label }}" class="form-label">联系电话</label>
                                {{ form.phone_number }}
                                {% if form.phone_number.errors %}
                                <div class="text-danger">{{ form.phone_number.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.purchase_date.id_for_label }}" class="form-label">购买日期</label>
                                {{ form.purchase_date }}
                                {% if form.purchase_date.errors %}
                                <div class="text-danger">{{ form.purchase_date.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.color.id_for_label }}" class="form-label">颜色</label>
                                {{ form.color }}
                                {% if form.color.errors %}
                                <div class="text-danger">{{ form.color.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>

                    {% if college_user and college_user.is_super_admin %}
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.college.id_for_label }}" class="form-label">所属学院 *</label>
                                {{ form.college }}
                                <div class="form-text">
                                    <small class="text-muted">
                                        <i class="bi bi-info-circle"></i> 
                                        超级管理员可以选择任意学院
                                    </small>
                                </div>
                                {% if form.college.errors %}
                                <div class="text-danger">{{ form.college.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    {% elif college_user and college_user.is_admin %}
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">所属学院</label>
                                <input type="text" class="form-control" value="{{ college_user.get_college_display }}" readonly>
                                <div class="form-text">
                                    <small class="text-muted">
                                        <i class="bi bi-info-circle"></i> 
                                        您只能添加本学院的自行车
                                    </small>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% else %}
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="{{ form.college.id_for_label }}" class="form-label">所属学院 *</label>
                                {{ form.college }}
                                <div class="form-text">
                                    <small class="text-muted">
                                        <i class="bi bi-info-circle"></i> 
                                        请选择您所属的学院
                                    </small>
                                </div>
                                {% if form.college.errors %}
                                <div class="text-danger">{{ form.college.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    {% endif %}

                    <div class="mb-3">
                        <label for="{{ form.image.id_for_label }}" class="form-label">自行车图片</label>
                        {{ form.image }}
                        <div class="form-text">
                            <small class="text-muted">
                                <i class="bi bi-info-circle"></i> 
                                图片将自动压缩至200KB以下，建议上传清晰的照片
                            </small>
                        </div>
                        {% if form.image.errors %}
                        <div class="text-danger">{{ form.image.errors }}</div>
                        {% endif %}
                    </div>

                    <div class="mb-3">
                        <label for="{{ form.notes.id_for_label }}" class="form-label">备注</label>
                        {{ form.notes }}
                        {% if form.notes.errors %}
                        <div class="text-danger">{{ form.notes.errors }}</div>
                        {% endif %}
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-circle"></i> 保存
                        </button>
                        <a href="{% url 'bike:admin_bike_list' %}" class="btn btn-secondary">
                            <i class="bi bi-x-circle"></i> 取消
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}
